package Test_0214;

import java.util.*;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Test {
    public static void main(String[] args) {
        Set<Integer> set1=new HashSet<>();
        //add：添加元素
        set1.add(5);
        set1.add(20);
        set1.add(15);
        set1.add(8);
        //remove:移除元素
        set1.remove(20);
        //是否包含该元素
        System.out.println(set1.contains(5));//true
        System.out.println(set1.contains(2));//false
        //Iterator:遍历集合
        Iterator<Integer> iterator=set1.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next()+" ");//5 8 15
        }
        System.out.println();
        //isEmpty：判断是否为空
        System.out.println(set1.isEmpty());//false：不为空
        //size:计算元素个数
        System.out.println(set1.size());//3
        //toArray：将set中的元素转换为数组
        Integer[] toArray=set1.toArray(new Integer[0]);
        for (Integer x:toArray) {
            System.out.print(x+" ");//5 8 15
        }
        System.out.println();
        //containsAll:set是否包含指定集合的所有元素
        Set<Integer> set2=new HashSet<>();
        set2.add(1);
        set2.add(2);
        set2.add(3);
        Set<Integer> set3=new HashSet<>();
        set3.add(1);
        set3.add(2);
        //看set2中是否都包含set3集合中的元素
        boolean containsAll=set2.containsAll(set3);
        System.out.println(containsAll);//true
        //addAll：将集合中的元素添加到set中
        Set<Integer> set4=new TreeSet<>();
        set4.add(1);
        set4.add(5);
        set4.add(19);
        Set<Integer> set5=new TreeSet<>();
        set5.add(2);
        set5.add(1);
        set5.add(6);
        boolean addAll=set4.addAll(set5);
        Iterator<Integer> iterator1=set4.iterator();
        while(iterator1.hasNext()){
            //如果是HashSet那么不自动排序，如果是TreeSet就自动排序
            System.out.print(iterator1.next()+" ");//1 2 5 6 19
        }
    }
    public static void main4(String[] args) {
        Map<String,Integer> map1=new TreeMap<>();
        map1.put("a",2);
        map1.put("a",3);
        map1.put("b",3);
        map1.put("c",6);
        //Set<K> keySet:返回key中不重复的集合
        Set<String> keySet=map1.keySet();
        System.out.println(keySet);//[a, b, c]
        //Collection<V> values:返回value中可重复的集合
        Collection<Integer> value=map1.values();
        System.out.println(value);//[3, 3, 6]
        //Set<K,V>> entrySet:返回key-value所有的映射关系
        Set<Map.Entry<String,Integer>> entrySet=map1.entrySet();
        System.out.println(entrySet);//[a=3, b=3, c=6]
    }
    public static void main3(String[] args) {
        Map<String,Integer> map1=new TreeMap<>();
        map1.put("a",null);
        map1.get("a");//无异常
        map1.put(null,2);
        map1.get(null);//报错
    }
    public static void main2(String[] args) {
        Map<String,Integer> map1=new HashMap<>();
        map1.put("a",null);
        map1.get("a");//无异常
        map1.put(null,null);
        map1.get(null);//无异常
    }
    public static void main1(String[] args) {
        Map<String,Integer> map=new HashMap<>();
        map.put("a",11);
        map.put("a",24);
        map.put("a",15);
        System.out.println(map.get("a"));//15
    }
}
